package com.example.ecas.controller;

import com.example.ecas.service.GetFilesService;
import com.example.ecas.vo.CaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;


import com.example.ecas.common.CommonResponse;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class GetFilesController {

    @Autowired
    private GetFilesService getFilesService;


    //识别图片
    @PostMapping(value = "/imgFiles")
    public CommonResponse<Object> identifyImage(@RequestParam("file") MultipartFile[] multipartFile) throws IOException {
        String outputDir = "./ecas/src/main/resources/upload";
        String pythonFilePath = "./ecas/src/main/python/ocr.py";
        StringBuilder message = new StringBuilder("");
        int length = multipartFile.length;

        for (int i = 0; i < length; i++) {
            String fileName = multipartFile[i].getOriginalFilename();
            String targetFile = outputDir + "/" + fileName;
            File file = getFilesService.storeFile(multipartFile[i], outputDir);
            CommonResponse<Object> commonResponse = getFilesService.identifyImage(file, pythonFilePath, targetFile);
            if (commonResponse.isSuccess()) {
                message.append("[").append(i + 1).append("]").append(fileName).append(" ").append("识别成功").append(";");
                System.out.println((String) commonResponse.getData());
                System.out.println(commonResponse.getCode());
                System.out.println(commonResponse.getMsg());
            } else {
                String msg = commonResponse.getMsg();
                message.append("[").append(i + 1).append("]").append(fileName).append(" ").append("识别失败").append(",").append(msg).append(";");
                System.out.println(msg);
            }
        }
        return CommonResponse.creatForSuccessMessage(message.toString());
    }


    //识别pdf
    @PostMapping(value = "/pdfFiles")
    public CommonResponse<Object> identifyPDF(@RequestParam("file") MultipartFile[] multipartFile) throws IOException {
        System.out.println("进入");
        String outputDir = "./ecas/src/main/resources/upload";
        StringBuilder message = new StringBuilder("");
        int length = multipartFile.length;
        for (int i = 0; i < length; i++) {
            System.out.println("--------------------------------------------------");
            String fileName = multipartFile[i].getOriginalFilename();
            File file = getFilesService.storeFile(multipartFile[i], outputDir);
            CommonResponse<Object> commonResponse = getFilesService.identifyPDF(file);

            if (commonResponse.isSuccess()) {
                message.append("[").append(i + 1).append("]").append(fileName).append(" ").append("识别成功").append(";");
                System.out.println((String) commonResponse.getData());
            } else {
                String msg = commonResponse.getMsg();
                message.append("[").append(i + 1).append("]").append(fileName).append(" ").append("识别失败").append(",").append(msg).append(";");
                System.out.println(msg);
            }

        }
        System.out.println(message);
        return CommonResponse.creatForSuccessMessage(message.toString());
    }

    //识别pdf
    @PostMapping(value = "/pdfFile")
    @ResponseBody
    public CommonResponse<Object> identifysinglePDF(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        System.out.println("进入");
        String outputDir = "./ecas/src/main/resources/upload";
        StringBuilder message = new StringBuilder("");
        System.out.println("--------------------------------------------------");
        String fileName = multipartFile.getOriginalFilename();
        File file = getFilesService.storeFile(multipartFile, outputDir);
        CommonResponse<Object> commonResponse = getFilesService.identifyPDF(file);


        String data=(String)commonResponse.getData();
        String []keywords={"患者姓名","年龄","身高","体重","性别","日期","是否结婚","民族","主诉","现病史","既往史","治疗方案","备注"};
        String []content=new String[keywords.length];

        int indexEnd=0;
        indexEnd=data.indexOf(keywords[0]);
        int indexStart=indexEnd+keywords[0].length();
        for(int i=1;i<keywords.length;i++){
            indexEnd=data.indexOf(keywords[i]);
            content[i-1]=data.substring(indexStart,indexEnd);
            indexStart=indexEnd+keywords[i].length();
        }
        content[keywords.length-1]=data.substring(indexStart,data.length());

        for(int i=0;i<content.length;i++){
            if(content[i].contains(":")){
                int index=content[i].indexOf(":");
                content[i]=content[i].substring(index+1);
            }else{
                if(content[i].contains("：")){
                    int index=content[i].indexOf("：");
                    content[i]=content[i].substring(index+1);
                }
            }

            content[i]=content[i].trim();
        }

        boolean married;
        if(content[6].contains("是")){
            married=true;
        }else {
            married= false;
        }


        Date date;
        try{
            date=Date.valueOf(content[5]);
        }catch (Exception e){
            date=null;
            System.out.println("日期转换失败");
        }


        int age=-1;
        double height=-1;
        double width=-1;
        try{
            age=Integer.parseInt(content[1]);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("年龄转换失败");
        }
        try{
            height=Double.parseDouble(content[2]);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("身高转换失败");
        }
        try{
            width=Double.parseDouble(content[3]);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("体重转换失败");
        }

        CaseVO caseVO=new CaseVO(-1,-1,content[0],age,height,width,content[4],date,married,content[7],
                content[8],content[9],content[10],content[11],content[12]);


        return CommonResponse.creatForSuccess(caseVO);
    }
}


